package com.srzk.youthleagueback.service;


import com.srzk.youthleagueback.mapper.MeetingMapper;
import com.srzk.youthleagueback.pojo.Meeting;
import com.srzk.youthleagueback.results.Result;
import com.srzk.youthleagueback.utils.ObjectUtil;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class MeetingService {
    @Autowired
    private MeetingMapper meetingMapper;


    public Result<Map<String, Long>> addMeeting(Meeting meeting) {
        int result = meetingMapper.insert(meeting);
        if (result == 1) {
            HashMap<String, Long> map = new HashMap<>();
            map.put("id", meeting.getId());
            return Result.success(map);
        } else {
            return Result.error(ADD_FAIL);
        }
    }

    public Result<String> deleteMeeting(String user, String year, int type, int id) {
        Meeting meeting = meetingMapper.selectByPrimaryKey(user, year, type, id);
        int result = meetingMapper.deleteByUserAndYearAndTypeAndId(user, year, type, id);
        if (result == 1) {
            return Result.success("删除成功");
        } else {
            return Result.error(DELETE_FAIL);
        }
    }

    public Result<String> updateMeeting(Meeting meeting) {
        int result = meetingMapper.updateByPrimaryKey(meeting);
        if (result == 1) {
            return Result.success("更新成功");
        } else {
            return Result.error(UPDATE_FAIL);
        }
    }

    public Result<List<Object>> getMeetingList(String user, String year, int type) {
        List<Meeting> meeting = meetingMapper.selectByPrimaryKeyList(user, year, type);
        if (meeting != null) {
            ArrayList<Object> meetingList = new ArrayList<>();
            meeting.forEach(items -> {
                Map<String, Object> listItem = new HashMap<>();
                Map<String, Object> info = new HashMap<>();
                info.put("date", items.getDate());
                info.put("address", items.getAddress());
                info.put("host", items.getHost());
                info.put("subject", items.getSubject());
                info.put("count", items.getCount());
                info.put("content", items.getContent());
                info.put("condition", items.getCondition());
                info.put("summary", items.getSummary());
                listItem.put("id", items.getId());
                listItem.put("meetingInfo", info);
                meetingList.add(listItem);
            });
            return Result.success(meetingList);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String, Object>> getGradeList(String grade, String year, int type) {
        List<Meeting> meeting = meetingMapper.selectByGrade(year, type);
        if (meeting != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();

            meeting.forEach(items -> {
                List<Map<String, Object>> list = new ArrayList<>();
                if (ObjectUtil.getGrade(items.getUser()).equals(grade)) {
                    String getClass = ObjectUtil.getClass(items.getUser());

                    Map<String, Object> listItem = new HashMap<>();
                    listItem.put("date", items.getDate());
                    listItem.put("address", items.getAddress());
                    listItem.put("host", items.getHost());
                    listItem.put("subject", items.getSubject());
                    listItem.put("count", items.getCount());
                    listItem.put("content", items.getContent());
                    listItem.put("condition", items.getCondition());
                    listItem.put("summary", items.getSummary());

                    boolean flag = false;
                    for (Map<String, Object> stringObjectMap : data) {
                        if (getClass.equals(stringObjectMap.get("class"))) {
                            ArrayList tempList = (ArrayList) stringObjectMap.get("list");
                            tempList.add(listItem);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        Map<String, Object> dataItem = new HashMap<>();
                        dataItem.put("class", getClass);
                        list.add(listItem);
                        dataItem.put("list", list);
                        data.add(dataItem);
                    }
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String, Object>> getHistoryList(String user, int type) {
        List<Meeting> addData = meetingMapper.selectAll(user, type);
        if (addData != null) {
            Map<String, Object> objectMap = new HashMap<>();
            ArrayList<Map<String, Object>> data = new ArrayList<>();
            addData.forEach(items -> {
                ArrayList<Map<String, Object>> list;
                Map<String, Object> listItem = new HashMap<>();
                listItem.put("date", items.getDate());
                listItem.put("address", items.getAddress());
                listItem.put("host", items.getHost());
                listItem.put("subject", items.getSubject());
                listItem.put("count", items.getCount());
                listItem.put("content", items.getContent());
                listItem.put("condition", items.getCondition());
                listItem.put("summary", items.getSummary());
                listItem.put("id", items.getId());
                boolean flag = false;
                for (Map<String, Object> datum : data) {
                    if(items.getYear().equals(datum.get("year"))){
                        list = (ArrayList<Map<String, Object>>) datum.get("list");
                        list.add(listItem);
                        flag = true;
                        break;
                    }
                }
                if(!flag){
                    list = new ArrayList<>();
                    Map<String, Object> dataItem = new HashMap<>();
                    dataItem.put("year", items.getYear());
                    list.add(listItem);
                    dataItem.put("list", list);
                    data.add(dataItem);
                }
            });
            objectMap.put("data", data);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }

    public Result<Map<String, Object>> getMeeting(String user, String year, int type, int id) {
        Meeting meeting = meetingMapper.selectByPrimaryKey(user, year, type, id);
        if (meeting != null) {
            Map<String, Object> info = new HashMap<>();
            info.put("date", meeting.getDate());
            info.put("address", meeting.getAddress());
            info.put("host", meeting.getHost());
            info.put("subject", meeting.getSubject());
            info.put("count", meeting.getCount());
            info.put("content", meeting.getContent());
            info.put("condition", meeting.getCondition());
            info.put("summary", meeting.getSummary());
            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("meetingInfo", info);
            Map<String, Object> data = new HashMap<>();
            data.put("data", objectMap);
            return Result.success(objectMap);
        }
        return Result.error(NOT_EXIST);
    }


}
